package syan.redisp.service.impl;

import syan.redisp.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author CHENTAIWEN
 * @create 2022/8/1 16:21
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


//    Publish 发布 subscription 订阅
    private static String key="Message:Order:";

    @Override
    public Object query(String channel) {
       return redisTemplate.opsForStream().range(key+channel, Range.from(Range.Bound.inclusive("-")).to(Range.Bound.inclusive("+")));
    }
    @Override
    public void add(String channel) {
        Map<String, Object> map = new HashMap<>();
        String mapId = System.currentTimeMillis()+"-0";
        map.put(mapId,"下单");
        redisTemplate.opsForStream().add(key+channel,map);
    }

    @Override
    public void update(String channel, String body) {
        Map<String, Object> map = new HashMap<>();
        String mapId = System.currentTimeMillis()+"-0";
        map.put(mapId,"下单");
        redisTemplate.opsForStream().add(key+channel,map);
    }

    @Override
    public void delete(String channel) {
        redisTemplate.opsForStream().range(channel, Range.from(Range.Bound.inclusive("-")).to(Range.Bound.inclusive("+")));

    }
}
